package com.echo.blog.core.utils;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

/**
 * 上传图片到Fast
 *
 * @author chentl
 */
@Slf4j
public class FastDFSUtils {

    /**
     * @param pic       图片二进制
     * @param file_name 图片名称
     * @param size      图片大小
     * @return
     * @Description: 上传图片到Fast.
     * @Title: uploadPic
     * @author: chentl
     * @date: 2017年12月14日
     */
    public static String uploadPic(byte[] pic, String file_name, long size) {
        String path = null;
        try {
            ClientGlobal.init("fdfs_client.conf");
            //老大客户端
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
            //图片11.jpg  根据图片名称得到图片后缀    jpg
            String ext = FilenameUtils.getExtension(file_name);

            NameValuePair[] meta_list = new NameValuePair[3];
            meta_list[0] = new NameValuePair("fileName", file_name);
            meta_list[1] = new NameValuePair("fileExt", ext);
            meta_list[2] = new NameValuePair("fileSize", String.valueOf(size));


            path = storageClient1.upload_file1(pic, ext, meta_list);
        } catch (Exception e) {
            log.error("上传到服务器时出错", e);
        }
        return path;
    }
}